<template>
  <div class="app-container"></div>
</template>

<script>
import {
  queryOption,
  createOption,
  updateOption,
  deleteOption,
} from "@/api/system/options";
import { titleList } from "@/api/system/title";
export default {
  name: "options",
  data() {
    return {
      // 搜索条显示
      searchToggle: true,
      // 遮罩层
      loading: true,
      // 菜单表格树数据
      dataResult: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 合计条数
      total: 0,
      // 查询参数
      queryParams: {
        queryText: undefined,
        pageIndex: 1,
        pageSize: 10,
        orderby: "updateTime",
        sort: "ascending",
        dictID: 0,
      },
      // 表单参数
      form: {
        dictID: "",
        label: "",
        value: "",
        sortIndex: "",
        remark: "",
        optionId: undefined,
      },
      /**父级列表 */
      parentList: [],
      // 表单校验
      rules: {
        dictID: [
          { required: true, message: "请选择父级标题", trigger: "blur" },
        ],
        label: [{ required: true, message: "lable不能为空", trigger: "blur" }],
        value: [{ required: true, message: "value不能为空", trigger: "blur" }],
        sortIndex: [
          { required: true, message: "排序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.ParentList();
  },
  methods: {
    handleSearch() {
      this.searchToggle = !this.searchToggle;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      queryOption(this.queryParams).then((response) => {
        response.data.dataSource.forEach((element) => {
          if (!element.remark) {
            element.remark = "- - - -";
          }
        });
        this.dataResult = response.data;
        this.total = this.dataResult.totalCount;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        optionId: undefined,
        dictID: "",
        label: undefined,
        value: undefined,
        sortIndex: 1,
        remark: undefined,
      };
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryParams.queryText = "";
      this.queryParams.dictID = 0;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 新增按钮操作 */
    handleCreate(row) {
      this.reset();
      this.open = true;
      this.title = "添加字典";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      console.log(row);
      this.title = "修改菜单";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.optionId !== undefined) {
            let json = this.form;
            json.id = json.optionId;
            updateOption(this.form).then((response) => {
              if (response.statusCode === 200) {
                this.$message({
                  message: "修改成功",
                  type: "success",
                });
                this.open = false;
                this.getList();
              }
            });
          } else {
            createOption(this.form).then((response) => {
              if (response.statusCode === 200) {
                this.$message({
                  message: "新增成功",
                  type: "success",
                });
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        '是否确认删除名称为"' + row.label + " - " + row.value + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deleteOption(row.optionId).then((response) => {
            if (response.statusCode === 200) {
              this.getList();
              this.$message({
                message: "删除成功",
                type: "success",
              });
            }
          });
        })
        .catch(function () {});
    },
    // 自动计算分页 Id
    handleIndexCalc(index) {
      return (
        (this.queryParams.pageIndex - 1) * this.queryParams.pageSize + index + 1
      );
    },
    /***获取所有父级标题 */
    async ParentList() {
      let { data, statusCode } = await titleList({
        label: "",
        // orderBy: "",
        pageIndex: 1,
        pageSize: 2000,
      });
      if (statusCode != 200) return;
      this.parentList = data.dataSource;
    },
    // 排序操作
    handleSortable(val) {
      if (val.prop == "dictLabel") {
        val.prop = "dictID";
      }
      console.log(val);
      this.queryParams.orderby = val.prop;
      this.queryParams.sort = val.order;
      this.getList();
    },
  },
};
</script>